/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.IllegalOrphanException;
import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T001User;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T003Pharmacist;
import java.util.ArrayList;
import java.util.Collection;
import ep.persistence.entities.T002Doctor;

/**
 *
 * @author Spyros
 */
public class T001UserJpaController {

    public T001UserJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T001User t001User) {
        if (t001User.getT003PharmacistCollection() == null) {
            t001User.setT003PharmacistCollection(new ArrayList<T003Pharmacist>());
        }
        if (t001User.getT002DoctorCollection() == null) {
            t001User.setT002DoctorCollection(new ArrayList<T002Doctor>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<T003Pharmacist> attachedT003PharmacistCollection = new ArrayList<T003Pharmacist>();
            for (T003Pharmacist t003PharmacistCollectionT003PharmacistToAttach : t001User.getT003PharmacistCollection()) {
                t003PharmacistCollectionT003PharmacistToAttach = em.getReference(t003PharmacistCollectionT003PharmacistToAttach.getClass(), t003PharmacistCollectionT003PharmacistToAttach.getId());
                attachedT003PharmacistCollection.add(t003PharmacistCollectionT003PharmacistToAttach);
            }
            t001User.setT003PharmacistCollection(attachedT003PharmacistCollection);
            Collection<T002Doctor> attachedT002DoctorCollection = new ArrayList<T002Doctor>();
            for (T002Doctor t002DoctorCollectionT002DoctorToAttach : t001User.getT002DoctorCollection()) {
                t002DoctorCollectionT002DoctorToAttach = em.getReference(t002DoctorCollectionT002DoctorToAttach.getClass(), t002DoctorCollectionT002DoctorToAttach.getId());
                attachedT002DoctorCollection.add(t002DoctorCollectionT002DoctorToAttach);
            }
            t001User.setT002DoctorCollection(attachedT002DoctorCollection);
            em.persist(t001User);
            for (T003Pharmacist t003PharmacistCollectionT003Pharmacist : t001User.getT003PharmacistCollection()) {
                T001User oldT001UserOfT003PharmacistCollectionT003Pharmacist = t003PharmacistCollectionT003Pharmacist.getT001User();
                t003PharmacistCollectionT003Pharmacist.setT001User(t001User);
                t003PharmacistCollectionT003Pharmacist = em.merge(t003PharmacistCollectionT003Pharmacist);
                if (oldT001UserOfT003PharmacistCollectionT003Pharmacist != null) {
                    oldT001UserOfT003PharmacistCollectionT003Pharmacist.getT003PharmacistCollection().remove(t003PharmacistCollectionT003Pharmacist);
                    oldT001UserOfT003PharmacistCollectionT003Pharmacist = em.merge(oldT001UserOfT003PharmacistCollectionT003Pharmacist);
                }
            }
            for (T002Doctor t002DoctorCollectionT002Doctor : t001User.getT002DoctorCollection()) {
                T001User oldT001UserOfT002DoctorCollectionT002Doctor = t002DoctorCollectionT002Doctor.getT001User();
                t002DoctorCollectionT002Doctor.setT001User(t001User);
                t002DoctorCollectionT002Doctor = em.merge(t002DoctorCollectionT002Doctor);
                if (oldT001UserOfT002DoctorCollectionT002Doctor != null) {
                    oldT001UserOfT002DoctorCollectionT002Doctor.getT002DoctorCollection().remove(t002DoctorCollectionT002Doctor);
                    oldT001UserOfT002DoctorCollectionT002Doctor = em.merge(oldT001UserOfT002DoctorCollectionT002Doctor);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T001User t001User) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T001User persistentT001User = em.find(T001User.class, t001User.getId());
            Collection<T003Pharmacist> t003PharmacistCollectionOld = persistentT001User.getT003PharmacistCollection();
            Collection<T003Pharmacist> t003PharmacistCollectionNew = t001User.getT003PharmacistCollection();
            Collection<T002Doctor> t002DoctorCollectionOld = persistentT001User.getT002DoctorCollection();
            Collection<T002Doctor> t002DoctorCollectionNew = t001User.getT002DoctorCollection();
            List<String> illegalOrphanMessages = null;
            for (T003Pharmacist t003PharmacistCollectionOldT003Pharmacist : t003PharmacistCollectionOld) {
                if (!t003PharmacistCollectionNew.contains(t003PharmacistCollectionOldT003Pharmacist)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain T003Pharmacist " + t003PharmacistCollectionOldT003Pharmacist + " since its t001User field is not nullable.");
                }
            }
            for (T002Doctor t002DoctorCollectionOldT002Doctor : t002DoctorCollectionOld) {
                if (!t002DoctorCollectionNew.contains(t002DoctorCollectionOldT002Doctor)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain T002Doctor " + t002DoctorCollectionOldT002Doctor + " since its t001User field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<T003Pharmacist> attachedT003PharmacistCollectionNew = new ArrayList<T003Pharmacist>();
            for (T003Pharmacist t003PharmacistCollectionNewT003PharmacistToAttach : t003PharmacistCollectionNew) {
                t003PharmacistCollectionNewT003PharmacistToAttach = em.getReference(t003PharmacistCollectionNewT003PharmacistToAttach.getClass(), t003PharmacistCollectionNewT003PharmacistToAttach.getId());
                attachedT003PharmacistCollectionNew.add(t003PharmacistCollectionNewT003PharmacistToAttach);
            }
            t003PharmacistCollectionNew = attachedT003PharmacistCollectionNew;
            t001User.setT003PharmacistCollection(t003PharmacistCollectionNew);
            Collection<T002Doctor> attachedT002DoctorCollectionNew = new ArrayList<T002Doctor>();
            for (T002Doctor t002DoctorCollectionNewT002DoctorToAttach : t002DoctorCollectionNew) {
                t002DoctorCollectionNewT002DoctorToAttach = em.getReference(t002DoctorCollectionNewT002DoctorToAttach.getClass(), t002DoctorCollectionNewT002DoctorToAttach.getId());
                attachedT002DoctorCollectionNew.add(t002DoctorCollectionNewT002DoctorToAttach);
            }
            t002DoctorCollectionNew = attachedT002DoctorCollectionNew;
            t001User.setT002DoctorCollection(t002DoctorCollectionNew);
            t001User = em.merge(t001User);
            for (T003Pharmacist t003PharmacistCollectionNewT003Pharmacist : t003PharmacistCollectionNew) {
                if (!t003PharmacistCollectionOld.contains(t003PharmacistCollectionNewT003Pharmacist)) {
                    T001User oldT001UserOfT003PharmacistCollectionNewT003Pharmacist = t003PharmacistCollectionNewT003Pharmacist.getT001User();
                    t003PharmacistCollectionNewT003Pharmacist.setT001User(t001User);
                    t003PharmacistCollectionNewT003Pharmacist = em.merge(t003PharmacistCollectionNewT003Pharmacist);
                    if (oldT001UserOfT003PharmacistCollectionNewT003Pharmacist != null && !oldT001UserOfT003PharmacistCollectionNewT003Pharmacist.equals(t001User)) {
                        oldT001UserOfT003PharmacistCollectionNewT003Pharmacist.getT003PharmacistCollection().remove(t003PharmacistCollectionNewT003Pharmacist);
                        oldT001UserOfT003PharmacistCollectionNewT003Pharmacist = em.merge(oldT001UserOfT003PharmacistCollectionNewT003Pharmacist);
                    }
                }
            }
            for (T002Doctor t002DoctorCollectionNewT002Doctor : t002DoctorCollectionNew) {
                if (!t002DoctorCollectionOld.contains(t002DoctorCollectionNewT002Doctor)) {
                    T001User oldT001UserOfT002DoctorCollectionNewT002Doctor = t002DoctorCollectionNewT002Doctor.getT001User();
                    t002DoctorCollectionNewT002Doctor.setT001User(t001User);
                    t002DoctorCollectionNewT002Doctor = em.merge(t002DoctorCollectionNewT002Doctor);
                    if (oldT001UserOfT002DoctorCollectionNewT002Doctor != null && !oldT001UserOfT002DoctorCollectionNewT002Doctor.equals(t001User)) {
                        oldT001UserOfT002DoctorCollectionNewT002Doctor.getT002DoctorCollection().remove(t002DoctorCollectionNewT002Doctor);
                        oldT001UserOfT002DoctorCollectionNewT002Doctor = em.merge(oldT001UserOfT002DoctorCollectionNewT002Doctor);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t001User.getId();
                if (findT001User(id) == null) {
                    throw new NonexistentEntityException("The t001User with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T001User t001User;
            try {
                t001User = em.getReference(T001User.class, id);
                t001User.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t001User with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<T003Pharmacist> t003PharmacistCollectionOrphanCheck = t001User.getT003PharmacistCollection();
            for (T003Pharmacist t003PharmacistCollectionOrphanCheckT003Pharmacist : t003PharmacistCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This T001User (" + t001User + ") cannot be destroyed since the T003Pharmacist " + t003PharmacistCollectionOrphanCheckT003Pharmacist + " in its t003PharmacistCollection field has a non-nullable t001User field.");
            }
            Collection<T002Doctor> t002DoctorCollectionOrphanCheck = t001User.getT002DoctorCollection();
            for (T002Doctor t002DoctorCollectionOrphanCheckT002Doctor : t002DoctorCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This T001User (" + t001User + ") cannot be destroyed since the T002Doctor " + t002DoctorCollectionOrphanCheckT002Doctor + " in its t002DoctorCollection field has a non-nullable t001User field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(t001User);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T001User> findT001UserEntities() {
        return findT001UserEntities(true, -1, -1);
    }

    public List<T001User> findT001UserEntities(int maxResults, int firstResult) {
        return findT001UserEntities(false, maxResults, firstResult);
    }

    private List<T001User> findT001UserEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T001User.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T001User findT001User(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T001User.class, id);
        } finally {
            em.close();
        }
    }

    public int getT001UserCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T001User> rt = cq.from(T001User.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
